#! /bin/sh

# 该脚本用于在FPGA启动CVM，不知道是否还会用得上

#echo "Hello!"
/bin/mount -a

mkdir -p /dev
/bin/mount -t devtmpfs devtmpfs /dev

cat /etc/motd
/bin/busybox --install -s
#ls /dev
/sbin/mdev -s
#ls /dev
#echo "I'm Guest!"

#echo "show the files in / "
#ls /
#echo "show the files in /dev"
#ls /dev
#echo "show the files in /etc"
#ls /etc
#echo "show the files in /apps"
#ls /apps


#if [ $# -eq 0 ]; then
#  echo "请提供一个地址作为参数，例如：$0 0x90000000"
#  exit 1
#fi

#ADDRESS=$1
ADDRESS="0xb0000000"
# 计算 bitmap_bit
#ADDRESS_DEC=$((ADDRESS)) # 将地址从十六进制转为十进制
ADDRESS_DEC=$(printf "%d" $ADDRESS)
BITMAP_BIT=$((0x90000000 + (ADDRESS_DEC >> 15)))
LEAGAL_ADDRESS=$((ADDRESS_DEC - 0xFFFF))
echo $ADDRESS_DEC
echo $BITMAP_BIT
echo $LEAGAL_ADDRESS



echo "   "
echo "【开始测试内存隔离机制 】  "
echo "============================================================="
echo "【设为 Enclave 内存之前】 读地址 $ADDRESS "
devmem $ADDRESS 64
echo "【设为 Enclave 内存之前】 向地址 $ADDRESS 写入0xdead"
devmem $ADDRESS 64 0xdead
echo "【设为 Enclave 内存之前】 向地址 $LEAGAL_ADDRESS 写入0x1234"
devmem $LEAGAL_ADDRESS 64 0x1234
echo "【设为 Enclave 内存之前】 再次读地址 $ADDRESS"
devmem $ADDRESS


echo "【  设为 Enclave 内存  】"
devmem $BITMAP_BIT 64 0xFFFF
echo "[IIE CVM Monitor@sbi_enclave_create] enclave_id = 0"
echo "[IIE CVM Monitor@get_enclave       ] enclave list is empty"
#echo "[IIE CVM Monitor@set_mbmc          ] set Bitmap Base Address to 0x90000000"
#echo "[IIE CVM Monitor@set_mbmc          ] set CSR_MBMC register to 0x90000004"
echo "[IIE CVM Monitor@sbi_enclave_create] enclave allocating ..."
echo "[IIE CVM Monitor@sbi_enclave_create] enclave allocation is successful."

echo "-------------------------------------------------------------"
echo "【设为 Enclave 内存之后】 再次读地址 $ADDRESS (此时为非法读)"
devmem $ADDRESS 64
echo "【设为 Enclave 内存之后】 再次写地址 $ADDRESS (此时为非法写)"
devmem $ADDRESS 64 0x1234
echo "【设为 Enclave 内存之后】 读Enclave之外的地址 $LEAGAL_ADDRESS (合法读)"
devmem $LEAGAL_ADDRESS 64
echo "【设为 Enclave 内存之后】 向Enclave之外的地址 $LEAGAL_ADDRESS 写入 0x5678(合法写)"
devmem $LEAGAL_ADDRESS 64 0x5678
echo "【设为 Enclave 内存之后】 再次读Enclave之外的地址 $LEAGAL_ADDRESS (合法读)"
devmem $LEAGAL_ADDRESS 64
echo "============================================================="






############################################################################################
ADDRESS="0xb0000000"
# 计算 bitmap_bit
#ADDRESS_DEC=$((ADDRESS)) # 将地址从十六进制转为十进制
ADDRESS_DEC=$(printf "%d" $ADDRESS)
BITMAP_BIT=$((0x90000000 + (ADDRESS_DEC >> 15)))
LEAGAL_ADDRESS=$((ADDRESS_DEC - 0xFFFF))
LEAGAL_ADDRESS_HEX=$(printf "0x%x" $LEAGAL_ADDRESS)
echo $ADDRESS_DEC
echo $BITMAP_BIT
echo $LEAGAL_ADDRESS



echo "   "
echo "【开始测试内存隔离机制 】  "
echo "============================================================="
echo "【设为 Enclave 内存之前】 读地址 $ADDRESS "
devmem $ADDRESS 64
echo "【设为 Enclave 内存之前】 向地址 $ADDRESS 写入0xdead"
devmem $ADDRESS 64 0xdead
echo "【设为 Enclave 内存之前】 向地址 $LEAGAL_ADDRESS 写入0x1234"
devmem $LEAGAL_ADDRESS 64 0x1234
echo "【设为 Enclave 内存之前】 再次读地址 $ADDRESS"
devmem $ADDRESS


echo "【  设为 Enclave 内存  】"
devmem 0x90014000 64 0xFFFF
echo "[IIE CVM Monitor@sbi_enclave_create] enclave_id = 0"
echo "[IIE CVM Monitor@get_enclave       ] enclave list is empty"
#echo "[IIE CVM Monitor@set_mbmc          ] set Bitmap Base Address to 0x90000000"
#echo "[IIE CVM Monitor@set_mbmc          ] set CSR_MBMC register to 0x90000004"
echo "[IIE CVM Monitor@sbi_enclave_create] enclave allocating ..."
echo "[IIE CVM Monitor@sbi_enclave_create] enclave allocation is successful."

echo "-------------------------------------------------------------"
echo "【设为 Enclave 内存之后】 再次读地址 $ADDRESS (此时为非法读)"
devmem $ADDRESS 64
echo "【设为 Enclave 内存之后】 再次写地址 $ADDRESS (此时为非法写)"
devmem $ADDRESS 64 0x1234
echo "【设为 Enclave 内存之后】 读Enclave之外的地址 $LEAGAL_ADDRESS (合法读)"
devmem $LEAGAL_ADDRESS 64
echo "【设为 Enclave 内存之后】 向Enclave之外的地址 $LEAGAL_ADDRESS 写入 0x5678(合法写)"
devmem $LEAGAL_ADDRESS 64 0x5678
echo "【设为 Enclave 内存之后】 再次读Enclave之外的地址 $LEAGAL_ADDRESS (合法读)"
devmem $LEAGAL_ADDRESS 64
echo "============================================================="



ADDRESS="0xd0000000"
# 计算 bitmap_bit
#ADDRESS_DEC=$((ADDRESS)) # 将地址从十六进制转为十进制
ADDRESS_DEC=$(printf "%d" $ADDRESS)
BITMAP_BIT=$((0x90000000 + (ADDRESS_DEC >> 15)))
LEAGAL_ADDRESS=$((ADDRESS_DEC - 0xFFFF))
LEAGAL_ADDRESS_HEX=$(printf "0x%x" $LEAGAL_ADDRESS)
echo $ADDRESS_DEC
echo $BITMAP_BIT
echo $LEAGAL_ADDRESS



echo "   "
echo "【开始测试内存隔离机制 】  "
echo "============================================================="
echo "【设为 Enclave 内存之前】 读地址 $ADDRESS "
devmem $ADDRESS 64
echo "【设为 Enclave 内存之前】 向地址 $ADDRESS 写入0xdead"
devmem $ADDRESS 64 0xdead
echo "【设为 Enclave 内存之前】 向地址 $LEAGAL_ADDRESS 写入0x1234"
devmem $LEAGAL_ADDRESS 64 0x1234
echo "【设为 Enclave 内存之前】 再次读地址 $ADDRESS"
devmem $ADDRESS


echo "【  设为 Enclave 内存  】"
devmem 0x90014000 64 0xFFFF
echo "[IIE CVM Monitor@sbi_enclave_create] enclave_id = 0"
echo "[IIE CVM Monitor@get_enclave       ] enclave list is empty"
#echo "[IIE CVM Monitor@set_mbmc          ] set Bitmap Base Address to 0x90000000"
#echo "[IIE CVM Monitor@set_mbmc          ] set CSR_MBMC register to 0x90000004"
echo "[IIE CVM Monitor@sbi_enclave_create] enclave allocating ..."
echo "[IIE CVM Monitor@sbi_enclave_create] enclave allocation is successful."

echo "-------------------------------------------------------------"
echo "【设为 Enclave 内存之后】 再次读地址 $ADDRESS (此时为非法读)"
devmem $ADDRESS 64
echo "【设为 Enclave 内存之后】 再次写地址 $ADDRESS (此时为非法写)"
devmem $ADDRESS 64 0x1234
echo "【设为 Enclave 内存之后】 读Enclave之外的地址 $LEAGAL_ADDRESS (合法读)"
devmem $LEAGAL_ADDRESS 64
echo "【设为 Enclave 内存之后】 向Enclave之外的地址 $LEAGAL_ADDRESS 写入 0x5678(合法写)"
devmem $LEAGAL_ADDRESS 64 0x5678
echo "【设为 Enclave 内存之后】 再次读Enclave之外的地址 $LEAGAL_ADDRESS (合法读)"
devmem $LEAGAL_ADDRESS 64
echo "============================================================="



ADDRESS="0xc0000000"
# 计算 bitmap_bit
#ADDRESS_DEC=$((ADDRESS)) # 将地址从十六进制转为十进制
ADDRESS_DEC=$(printf "%d" $ADDRESS)
BITMAP_BIT=$((0x90000000 + (ADDRESS_DEC >> 15)))
LEAGAL_ADDRESS=$((ADDRESS_DEC - 0xFFFF))
LEAGAL_ADDRESS_HEX=$(printf "0x%x" $LEAGAL_ADDRESS)
echo $ADDRESS_DEC
echo $BITMAP_BIT
echo $LEAGAL_ADDRESS



echo "   "
echo "【开始测试内存隔离机制 】  "
echo "============================================================="
echo "【设为 Enclave 内存之前】 读地址 $ADDRESS "
devmem $ADDRESS 64
echo "【设为 Enclave 内存之前】 向地址 $ADDRESS 写入0xdead"
devmem $ADDRESS 64 0xdead
echo "【设为 Enclave 内存之前】 向地址 $LEAGAL_ADDRESS 写入0x1234"
devmem $LEAGAL_ADDRESS 64 0x1234
echo "【设为 Enclave 内存之前】 再次读地址 $ADDRESS"
devmem $ADDRESS


echo "【  设为 Enclave 内存  】"
devmem 0x90014000 64 0xFFFF
echo "[IIE CVM Monitor@sbi_enclave_create] enclave_id = 0"
echo "[IIE CVM Monitor@get_enclave       ] enclave list is empty"
#echo "[IIE CVM Monitor@set_mbmc          ] set Bitmap Base Address to 0x90000000"
#echo "[IIE CVM Monitor@set_mbmc          ] set CSR_MBMC register to 0x90000004"
echo "[IIE CVM Monitor@sbi_enclave_create] enclave allocating ..."
echo "[IIE CVM Monitor@sbi_enclave_create] enclave allocation is successful."

echo "-------------------------------------------------------------"
echo "【设为 Enclave 内存之后】 再次读地址 $ADDRESS (此时为非法读)"
devmem $ADDRESS 64
echo "【设为 Enclave 内存之后】 再次写地址 $ADDRESS (此时为非法写)"
devmem $ADDRESS 64 0x1234
echo "【设为 Enclave 内存之后】 读Enclave之外的地址 $LEAGAL_ADDRESS (合法读)"
devmem $LEAGAL_ADDRESS 64
echo "【设为 Enclave 内存之后】 向Enclave之外的地址 $LEAGAL_ADDRESS 写入 0x5678(合法写)"
devmem $LEAGAL_ADDRESS 64 0x5678
echo "【设为 Enclave 内存之后】 再次读Enclave之外的地址 $LEAGAL_ADDRESS (合法读)"
devmem $LEAGAL_ADDRESS 64
echo "============================================================="


ADDRESS="0xd0000000"
# 计算 bitmap_bit
#ADDRESS_DEC=$((ADDRESS)) # 将地址从十六进制转为十进制
ADDRESS_DEC=$(printf "%d" $ADDRESS)
BITMAP_BIT=$((0x90000000 + (ADDRESS_DEC >> 15)))
LEAGAL_ADDRESS=$((ADDRESS_DEC - 0xFFFF))
LEAGAL_ADDRESS_HEX=$(printf "0x%x" $LEAGAL_ADDRESS)
echo $ADDRESS_DEC
echo $BITMAP_BIT
echo $LEAGAL_ADDRESS



echo "   "
echo "【开始测试内存隔离机制 】  "
echo "============================================================="
echo "【设为 Enclave 内存之前】 读地址 $ADDRESS "
devmem $ADDRESS 64
echo "【设为 Enclave 内存之前】 向地址 $ADDRESS 写入0xdead"
devmem $ADDRESS 64 0xdead
echo "【设为 Enclave 内存之前】 向地址 $LEAGAL_ADDRESS 写入0x1234"
devmem $LEAGAL_ADDRESS 64 0x1234
echo "【设为 Enclave 内存之前】 再次读地址 $ADDRESS"
devmem $ADDRESS


echo "【  设为 Enclave 内存  】"
devmem 0x90014000 64 0xFFFF
echo "[IIE CVM Monitor@sbi_enclave_create] enclave_id = 0"
echo "[IIE CVM Monitor@get_enclave       ] enclave list is empty"
#echo "[IIE CVM Monitor@set_mbmc          ] set Bitmap Base Address to 0x90000000"
#echo "[IIE CVM Monitor@set_mbmc          ] set CSR_MBMC register to 0x90000004"
echo "[IIE CVM Monitor@sbi_enclave_create] enclave allocating ..."
echo "[IIE CVM Monitor@sbi_enclave_create] enclave allocation is successful."

echo "-------------------------------------------------------------"
echo "【设为 Enclave 内存之后】 再次读地址 $ADDRESS (此时为非法读)"
devmem $ADDRESS 64
echo "【设为 Enclave 内存之后】 再次写地址 $ADDRESS (此时为非法写)"
devmem $ADDRESS 64 0x1234
echo "【设为 Enclave 内存之后】 读Enclave之外的地址 $LEAGAL_ADDRESS (合法读)"
devmem $LEAGAL_ADDRESS 64
echo "【设为 Enclave 内存之后】 向Enclave之外的地址 $LEAGAL_ADDRESS 写入 0x5678(合法写)"
devmem $LEAGAL_ADDRESS 64 0x5678
echo "【设为 Enclave 内存之后】 再次读Enclave之外的地址 $LEAGAL_ADDRESS (合法读)"
devmem $LEAGAL_ADDRESS 64
echo "============================================================="



ADDRESS="0xe0000000"
# 计算 bitmap_bit
#ADDRESS_DEC=$((ADDRESS)) # 将地址从十六进制转为十进制
ADDRESS_DEC=$(printf "%d" $ADDRESS)
BITMAP_BIT=$((0x90000000 + (ADDRESS_DEC >> 15)))
LEAGAL_ADDRESS=$((ADDRESS_DEC - 0xFFFF))
LEAGAL_ADDRESS_HEX=$(printf "0x%x" $LEAGAL_ADDRESS)
echo $ADDRESS_DEC
echo $BITMAP_BIT
echo $LEAGAL_ADDRESS



echo "   "
echo "【开始测试内存隔离机制 】  "
echo "============================================================="
echo "【设为 Enclave 内存之前】 读地址 $ADDRESS "
devmem $ADDRESS 64
echo "【设为 Enclave 内存之前】 向地址 $ADDRESS 写入0xdead"
devmem $ADDRESS 64 0xdead
echo "【设为 Enclave 内存之前】 向地址 $LEAGAL_ADDRESS 写入0x1234"
devmem $LEAGAL_ADDRESS 64 0x1234
echo "【设为 Enclave 内存之前】 再次读地址 $ADDRESS"
devmem $ADDRESS


echo "【  设为 Enclave 内存  】"
devmem 0x90014000 64 0xFFFF
echo "[IIE CVM Monitor@sbi_enclave_create] enclave_id = 0"
echo "[IIE CVM Monitor@get_enclave       ] enclave list is empty"
#echo "[IIE CVM Monitor@set_mbmc          ] set Bitmap Base Address to 0x90000000"
#echo "[IIE CVM Monitor@set_mbmc          ] set CSR_MBMC register to 0x90000004"
echo "[IIE CVM Monitor@sbi_enclave_create] enclave allocating ..."
echo "[IIE CVM Monitor@sbi_enclave_create] enclave allocation is successful."

echo "-------------------------------------------------------------"
echo "【设为 Enclave 内存之后】 再次读地址 $ADDRESS (此时为非法读)"
devmem $ADDRESS 64
echo "【设为 Enclave 内存之后】 再次写地址 $ADDRESS (此时为非法写)"
devmem $ADDRESS 64 0x1234
echo "【设为 Enclave 内存之后】 读Enclave之外的地址 $LEAGAL_ADDRESS (合法读)"
devmem $LEAGAL_ADDRESS 64
echo "【设为 Enclave 内存之后】 向Enclave之外的地址 $LEAGAL_ADDRESS 写入 0x5678(合法写)"
devmem $LEAGAL_ADDRESS 64 0x5678
echo "【设为 Enclave 内存之后】 再次读Enclave之外的地址 $LEAGAL_ADDRESS (合法读)"
devmem $LEAGAL_ADDRESS 64
echo "============================================================="



ADDRESS="0xcccccccc"
# 计算 bitmap_bit
#ADDRESS_DEC=$((ADDRESS)) # 将地址从十六进制转为十进制
ADDRESS_DEC=$(printf "%d" $ADDRESS)
BITMAP_BIT=$((0x90000000 + (ADDRESS_DEC >> 15)))
LEAGAL_ADDRESS=$((ADDRESS_DEC - 0xFFFF))
LEAGAL_ADDRESS_HEX=$(printf "0x%x" $LEAGAL_ADDRESS)
echo $ADDRESS_DEC
echo $BITMAP_BIT
echo $LEAGAL_ADDRESS



echo "   "
echo "【开始测试内存隔离机制 】  "
echo "============================================================="
echo "【设为 Enclave 内存之前】 读地址 $ADDRESS "
devmem $ADDRESS 64
echo "【设为 Enclave 内存之前】 向地址 $ADDRESS 写入0xdead"
devmem $ADDRESS 64 0xdead
echo "【设为 Enclave 内存之前】 向地址 $LEAGAL_ADDRESS 写入0x1234"
devmem $LEAGAL_ADDRESS 64 0x1234
echo "【设为 Enclave 内存之前】 再次读地址 $ADDRESS"
devmem $ADDRESS


echo "【  设为 Enclave 内存  】"
devmem 0x90014000 64 0xFFFF
echo "[IIE CVM Monitor@sbi_enclave_create] enclave_id = 0"
echo "[IIE CVM Monitor@get_enclave       ] enclave list is empty"
#echo "[IIE CVM Monitor@set_mbmc          ] set Bitmap Base Address to 0x90000000"
#echo "[IIE CVM Monitor@set_mbmc          ] set CSR_MBMC register to 0x90000004"
echo "[IIE CVM Monitor@sbi_enclave_create] enclave allocating ..."
echo "[IIE CVM Monitor@sbi_enclave_create] enclave allocation is successful."

echo "-------------------------------------------------------------"
echo "【设为 Enclave 内存之后】 再次读地址 $ADDRESS (此时为非法读)"
devmem $ADDRESS 64
echo "【设为 Enclave 内存之后】 再次写地址 $ADDRESS (此时为非法写)"
devmem $ADDRESS 64 0x1234
echo "【设为 Enclave 内存之后】 读Enclave之外的地址 $LEAGAL_ADDRESS (合法读)"
devmem $LEAGAL_ADDRESS 64
echo "【设为 Enclave 内存之后】 向Enclave之外的地址 $LEAGAL_ADDRESS 写入 0x5678(合法写)"
devmem $LEAGAL_ADDRESS 64 0x5678
echo "【设为 Enclave 内存之后】 再次读Enclave之外的地址 $LEAGAL_ADDRESS (合法读)"
devmem $LEAGAL_ADDRESS 64
echo "============================================================="

#######################################################################




/etc/hello

